//路径为'@/apis/settings'
// @/apis/settings.js

import axiosInstance from './axiosInstance';

const settingsAPI = {
  /**
   * 获取所有系统设置
   * @returns {Promise<Object>} 包含所有系统设置的对象
   */
  getAllSettings: async () => {
    try {
      const response = await axiosInstance.get('/api/admin/settings');
      return response;
    } catch (error) {
      console.error('获取系统设置失败:', error);
      throw error;
    }
  },

  /**
   * 更新系统设置
   * @param {Object} settings - 要更新的设置对象
   * @returns {Promise<string>} 更新成功的消息
   */
  updateSettings: async (settings) => {
    try {
      const response = await axiosInstance.put('/api/admin/settings', settings);
      return response;
    } catch (error) {
      console.error('更新系统设置失败:', error);
      throw error;
    }
  },

  /**
   * 获取单个系统设置
   * @param {string} key - 设置的键名
   * @returns {Promise<Object>} 包含单个设置的对象
   */
  getSetting: async (key) => {
    try {
      const response = await axiosInstance.get(`/api/admin/settings/${key}`);
      return response;
    } catch (error) {
      console.error(`获取设置 ${key} 失败:`, error);
      throw error;
    }
  },

  /**
   * 更新单个系统设置
   * @param {string} key - 设置的键名
   * @param {*} value - 设置的新值
   * @returns {Promise<string>} 更新成功的消息
   */
  updateSetting: async (key, value) => {
    try {
      const response = await axiosInstance.put(`/api/admin/settings/${key}`, { value });
      return response;
    } catch (error) {
      console.error(`更新设置 ${key} 失败:`, error);
      throw error;
    }
  },

  getWebsiteName: async () => {
    try {
      const response = await axiosInstance.get('/api/admin/settings');
      console.log('Settings response:', response);
      
      if (response && response.websiteName) {
        return response.websiteName;
      }
      return '在线订购系统'; // 如果 websiteName 不存在，返回默认值
    } catch (error) {
      console.error('获取网站名称失败:', error);
      return '在线订购系统'; // 返回默认值
    }
  },
};

  
export default settingsAPI;